package com.bambooshoot.service;

import com.bambooshoot.common.core.page.PageQuery;
import com.bambooshoot.common.core.page.TableDataInfo;
import com.bambooshoot.common.utils.MapstructUtils;
import com.bambooshoot.common.utils.StringUtils;
import com.bambooshoot.domain.BizIncomeDetails;
import com.bambooshoot.domain.bo.BizIncomeDetailsBo;
import com.bambooshoot.domain.vo.BizIncomeDetailsVo;
import com.bambooshoot.mapper.BizIncomeDetailsMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 收入明细Service业务层处理
 *
 * @author mxc
 * @date 2024-07-03
 */
@RequiredArgsConstructor
@Service
public class BizIncomeDetailsService extends ServiceImpl<BizIncomeDetailsMapper, BizIncomeDetails> {

    private final BizIncomeMonthlySummaryService bizMonthlyIncomeSummaryService;
    private final BizIncomeYearlySummaryService bizYearlyIncomeSummaryService;

    /**
     * 查询收入明细
     *
     * @param id 主键
     * @return 收入明细
     */
    public BizIncomeDetailsVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询收入明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 收入明细分页列表
     */
    public TableDataInfo<BizIncomeDetailsVo> queryPageList(BizIncomeDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BizIncomeDetails> lqw = buildQueryWrapper(bo);
        Page<BizIncomeDetailsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的收入明细列表
     *
     * @param bo 查询条件
     * @return 收入明细列表
     */
    public List<BizIncomeDetailsVo> queryList(BizIncomeDetailsBo bo) {
        LambdaQueryWrapper<BizIncomeDetails> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BizIncomeDetails> buildQueryWrapper(BizIncomeDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BizIncomeDetails> lqw = Wrappers.lambdaQuery();
        lqw.between(params.get("beginIncomeDay") != null && params.get("endIncomeDay") != null,
            BizIncomeDetails::getIncomeDay, params.get("beginIncomeDay"), params.get("endIncomeDay"));
        lqw.eq(bo.getAmount() != null, BizIncomeDetails::getAmount, bo.getAmount());
        lqw.ge(bo.getMinAmount() != null, BizIncomeDetails::getAmount, bo.getMinAmount());
        lqw.le(bo.getMaxAmount() != null, BizIncomeDetails::getAmount, bo.getMaxAmount());
        lqw.eq(bo.getType() != null, BizIncomeDetails::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), BizIncomeDetails::getRemark, bo.getRemark());
        lqw.orderByDesc(BizIncomeDetails::getIncomeDay);
        return lqw;
    }

    /**
     * 新增收入明细
     *
     * @param bo 收入明细
     * @return 是否新增成功
     */
    public Boolean insertByBo(BizIncomeDetailsBo bo) {
        BizIncomeDetails add = MapstructUtils.convert(bo, BizIncomeDetails.class);
        boolean flag = baseMapper.insert(add) > 0;
        handleDataAfterSave();
        return flag;
    }

    /**
     * 修改收入明细
     *
     * @param bo 收入明细
     * @return 是否修改成功
     */
    public Boolean updateByBo(BizIncomeDetailsBo bo) {
        BizIncomeDetails update = MapstructUtils.convert(bo, BizIncomeDetails.class);
        boolean flag = baseMapper.updateById(update) > 0;
        handleDataAfterSave();
        return flag;
    }

    /**
     * 保存完数据后处理其他数据
     */
    private void handleDataAfterSave() {
        bizMonthlyIncomeSummaryService.handleDataAfterSave();
        bizYearlyIncomeSummaryService.handleDataAfterSave();
    }

    /**
     * 校验并批量删除收入明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */

    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        boolean flag = baseMapper.deleteBatchIds(ids) > 0;
        bizMonthlyIncomeSummaryService.handleDataAfterSave();
        bizYearlyIncomeSummaryService.handleDataAfterSave();
        return flag;
    }
}
